import {
  reqAddressData,
  removeAddress
} from "../../../../api/address.js"
// import {swipeCell} from ("")
var swipeCell = require("../../../../behaviors/swipeCell.js")
const app = getApp()
Page({
  behaviors: [swipeCell],
  /**
   * 页面的初始数据
   */
  data: {
    addressList: [],
  },

  changeAddress(e) {
    if (this.flag !== "1") return
    const id = e.target.dataset.id
    const res = this.data.addressList.find((item) => {
      return item.id === id
    })
    if (res) {
      app.globalData.address = res
      wx.navigateBack()
    }
  },
  async getAddressData() {
    const res = await reqAddressData()
    this.setData({
      addressList: res.data
    })
  },
  onLoad(option) {
    console.log(option.flag);
    // 给全局的数据进行赋值
    this.flag = option.flag
  },
  onShow() {
    this.getAddressData()
  },
  // 去编辑页面
  toEdit(e) {
    const {
      dataset
    } = e.currentTarget
    wx.navigateTo({
      url: '/settingModule/pages/address/add/index?id=' + dataset.id
    })
  },
  // 删除地址
  async removeAdd(e) {
    const {
      dataset
    } = e.currentTarget
    const remove = await removeAddress(dataset.index)
    if (remove.code === 200) {
      wx.showToast({
        title: '删除成功',
      })
    } else {
      wx.showToast({
        title: '删除成功失败',
        icon: "error"
      })
    }
    // 更新数据
    this.getAddressData()
  },

})